/**
 * Description:菜单操作
 * @author lld
 * @version 1.0
 */
package com.douyu.water.core.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.douyu.water.core.DwzResult;
import com.douyu.water.core.PagePara;
import com.douyu.water.core.model.Menu;
import com.douyu.water.core.model.Role;
import com.douyu.water.core.service.MenuService;

@Controller
@RequestMapping("/menu/*")
public class MenuController {

	@Resource
	private MenuService menuService;

	/**
	 * 获取菜单列表
	 * 
	 * @param request
	 * @param menu
	 *            Menu
	 * @param page
	 *            PagePara
	 * @return
	 */
	@RequestMapping("/select")
	public String select(HttpServletRequest request, Menu menu, PagePara page) {
		String result = "WEB-INF/jsp/menu/list";
		try {
			List<Menu> list = menuService.select(menu, page);
			request.setAttribute("menuList", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("add")
	public String add(HttpServletRequest request) {
		String result = "WEB-INF/jsp/menu/add";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try {
			Menu menu = new Menu();
			menu.setCategory("1");
			menu.setUtype("1");
			List<Menu> list = menuService.select(menu, null);
			request.setAttribute("menu", list);
		} catch (Exception e) {
			e.printStackTrace();

		}
		return result;
	}

	/**
	 * 新建菜单
	 * 
	 * @param request
	 * @param menu
	 * @return
	 */
	@RequestMapping("/insert")
	public String insert(HttpServletRequest request, Menu menu) {
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try {
			int flag = menuService.insert(menu);
			switch (flag) {
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("添加失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("添加成功");
				dwzResult.setNavTabId("menuList");
				dwzResult.setCloseCurrent(false);
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("名称重复，添加失败");
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DwzResult.URL;
	}

	/**
	 * 修改页面
	 * 
	 * @param request
	 * @param record
	 *            Menu -菜单 ，只用ucode
	 * @return
	 */
	@RequestMapping("edit")
	public String edit(HttpServletRequest request, Menu record) {
		String result = "WEB-INF/jsp/menu/edit";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try {
			Menu menu = menuService.view(record);
			if (menu != null) {
				request.setAttribute("menu", menu);
				// 检索功能菜单
				Menu mu = new Menu();
				mu.setCategory("1");
				mu.setUtype("1");
				List<Menu> list = menuService.select(mu, null);
				request.setAttribute("menuList", list);
			} else {
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取对象");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param record
	 *            Menu
	 * @return
	 */
	@RequestMapping("/update")
	public String update(HttpServletRequest request, Menu record) {
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try {
			int flag = menuService.update(record);
			switch (flag) {
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("更新失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("更新成功");
				dwzResult.setCloseCurrent(false);
				dwzResult.setNavTabId("roleList");
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("名称重复，更新失败");
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}

	/**
	 * 删除菜单
	 * 
	 * @param request
	 * @param ucode
	 *            String -编号
	 * @return String
	 */
	@RequestMapping("/delete")
	public String delete(HttpServletRequest request, Menu record) {
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try {
			int flag = menuService.delete(record);
			switch (flag) {
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("删除失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("删除成功");
				dwzResult.setCloseCurrent(false);
				dwzResult.setNavTabId("roleList");
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("正在使用，无法删除");
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
}
